package com.doge.controller.admin;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.bo.aftersale.AftersaleBatchBO;
import com.doge.bean.pojo.aftersale.AfterSale;
import com.doge.exception.Param402Exception;
import com.doge.service.admin.aftersale.AftersaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * @ClassName: AftersaleController
 * @Description: 商场管理的  售后管理
 * @author: PuJing
 * @create: 2021-11-29-22:34
 **/
@RestController
@RequestMapping("admin/aftersale")
public class AftersaleController {

    @Autowired
    AftersaleService aftersaleService;


    /**
     * 查询指定条件的售后信息
     * http://182.92.235.201:8083/admin/aftersale/list?
     * page=1 & limit=20 & aftersaleSn=14 & orderId=15 & status= &sort=add_time&order=desc
     *
     * @param param
     * @param aftersaleSn 售后编号
     * @param orderId     订单Id
     * @param status      售后状态 0是可申请，1是用户已申请，2是管理员审核通过，3是管理员退款成功，4是管理员审核拒绝，5是用户已取消
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BaseParam param, String aftersaleSn, String orderId, Integer status) throws Param402Exception {

        // 接收到的aftersaleSn、orderId 可能是null，""，"xxx"
        // 接收到的status 可能是 null、1
        Integer aftersaleSnInteger = null;
        if (aftersaleSn != null && !"".equals(aftersaleSn)) {
            try {
                aftersaleSnInteger = Integer.parseInt(aftersaleSn);
            } catch (Exception e) {
                throw new Param402Exception("参数值不对");
            }
        }

        Integer orderIdInteger = null;
        if (orderId != null && !"".equals(orderId)) {
            try {
                orderIdInteger = Integer.parseInt(orderId);
            } catch (Exception e) {
                throw new Param402Exception("参数值不对");
            }
        }
        // 转化后的aftersaleSnInteger、orderIdInteger 可能是null，x
        // 接收到的status 可能是 null、1
        BaseData data = aftersaleService.list(param, aftersaleSnInteger, orderIdInteger, status);

        return BaseRespVo.ok(data);
    }

    /**
     *  recept通过  审核通过退款请求，status从1用户已申请 变成2管理员审核通过
     * @param afterSale 接收为订单POJO
     * @return
     */
    @RequestMapping("recept")
    public BaseRespVo recept(@RequestBody AfterSale afterSale) {

        Short status = 2;

        int code = aftersaleService.recept(afterSale,status);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "订单审核通过失败");
        }
    }

    /**
     * reject拒绝  拒绝退款请求，status从1用户已申请 变成4管理员审核拒绝
     * @param afterSale 接收为订单POJO
     * @return
     */
    @RequestMapping("reject")
    public BaseRespVo reject(@RequestBody AfterSale afterSale) {

        Short status = 4;

        int code = aftersaleService.reject(afterSale,status);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "订单审核拒绝失败");
        }
    }

    /**
     * 批量通过  审核通过退款请求，status从1用户已申请 变成2管理员审核通过
     * @param aftersaleBatchBO 接收要通过售后的id
     * @return
     */
    @RequestMapping("batch-recept")
    public BaseRespVo batch_recept(@RequestBody AftersaleBatchBO aftersaleBatchBO) {

        int code = aftersaleService.batch_recept(aftersaleBatchBO);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "订单审核拒绝失败");
        }
    }

    /**
     * 批量拒绝  拒绝退款请求，status从1用户已申请 变成4管理员审核拒绝
     * @param aftersaleBatchBO 接收要拒绝售后的id
     * @return
     */
    @RequestMapping("batch-reject")
    public BaseRespVo batch_reject(@RequestBody AftersaleBatchBO aftersaleBatchBO) {

        int code = aftersaleService.batch_reject(aftersaleBatchBO);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "订单审核拒绝失败");
        }
    }


    /**
     * 退款 status 变成3管理员退款成功
     * @param afterSale 接收为订单POJO
     * @return
     */
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody AfterSale afterSale){

        Short status = 3;

        int code = aftersaleService.refund(afterSale,status);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "订单退款失败");
        }

    }
}
